﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Power.Business;
using TjZheng.Power.DataEntity;

namespace TjZheng.CMS.Process.Business
{
    public partial class PowerProcess
    {
        /// <summary>
        /// 权限树
        /// </summary>
        /// <param name="id">根权限Id</param>
        /// <param name="isContainButton">是否包含按钮</param>
        /// <returns></returns>
        public TreeEntity BindPowerInfoTree(long id, bool isContainButton)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return null;
            }
            var objPowerInfoList = objBizPowerInfo.GetList(string.Format(isContainButton ? "IDPath like '{0}%'" : "IDPath like '{0}%' AND PowerType<>2", objPowerInfo.IDPath)).OrderBy(c => c.SortIndex).ToList();
            return objPowerInfoList.ConvertToTree(id);
        }
        ///<summary>
        ///除隐藏权限的权限树
        ///</summary>
        /// <param name="id">根权限Id</param>
        ///<param name="isContainButton">是否包含按钮</param>
        ///<returns></returns>
        public TreeEntity BindPowerInfoTreeWithoutUnDisplay(long id, bool isContainButton)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return null;
            }
            var objPowerInfoList = objBizPowerInfo.GetList(string.Format(isContainButton ? "IDPath like '{0}%' AND IsDisplay=1" : "IDPath like '{0}%' AND IsDisplay=1 AND PowerType<>2", objPowerInfo.IDPath)).OrderBy(c => c.SortIndex).ToList();
            return objPowerInfoList.ConvertToTree(id);
        }
        /// <summary>
        /// 权限树
        /// </summary>
        /// <param name="id">根权限Id</param>
        /// <param name="isContainButton">是否包含按钮</param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTrees(long id, bool isContainButton)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            var objPowerInfoList = objBizPowerInfo.GetList(string.Format(isContainButton ? "IDPath like '{0}%'" : "IDPath like '{0}%' AND PowerType<>2", objPowerInfo.IDPath)).OrderBy(c => c.SortIndex).ToList();
            return objPowerInfoList.ConvertToTreeList(id) ?? new List<TreeEntity>();
        }
        /// <summary>
        /// 除隐藏权限的权限树
        /// </summary>
        /// <param name="id">根权限Id</param>
        /// <param name="isContainButton">是否包含按钮</param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTreesWithoutUnDisplay(long id, bool isContainButton)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            var objPowerInfoList = objBizPowerInfo.GetList(string.Format(isContainButton ? "IDPath like '{0}%' AND IsDisplay=1" : "IDPath like '{0}%' AND IsDisplay=1 AND PowerType<>2", objPowerInfo.IDPath)).OrderBy(c => c.SortIndex).ToList();
            return objPowerInfoList.ConvertToTreeList(id) ?? new List<TreeEntity>();
        }
        /// <summary>
        /// 除根节点的权限列表树
        /// </summary>
        /// <param name="id">根权限Id</param>
        /// <param name="isContainButton">是否包含按钮</param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTreeList(long id, bool isContainButton)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            var objPowerInfoList = objBizPowerInfo.GetList(string.Format(isContainButton ? "IDPath like '{0}%'" : "IDPath like '{0}%' AND PowerType<>2", objPowerInfo.IDPath)).OrderBy(c => c.SortIndex).ToList();
            return objPowerInfoList.ConvertToTreeListExceptRoot(id) ?? new List<TreeEntity>();
        }
        /// <summary>
        /// 除根节点和隐藏权限的权限树
        /// </summary>
        /// <param name="id">根权限Id</param>
        /// <param name="isContainButton">是否包含按钮</param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoTreeListWithoutUnDisplay(long id, bool isContainButton)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            var objPowerInfoList = objBizPowerInfo.GetList(string.Format(isContainButton ? "IDPath like '{0}%' AND IsDisplay=1" : "IDPath like '{0}%' AND IsDisplay=1 AND PowerType<>2", objPowerInfo.IDPath)).OrderBy(c => c.SortIndex).ToList();
            return objPowerInfoList.ConvertToTreeListExceptRoot(id) ?? new List<TreeEntity>();
        }
        /// <summary>
        /// 除父节点和按钮的子权限列表
        /// </summary>
        /// <param name="id">父权限Id体</param>
        /// <param name="isContainUnDisplay">是否包含隐藏的权限</param>
        /// <returns></returns>
        public List<TreeEntity> BindPowerInfoChildrenWithoutButton(long id, bool isContainUnDisplay)
        {
            BizPowerInfo objBizPowerInfo = new BizPowerInfo();
            PowerInfo objPowerInfo = objBizPowerInfo.GetRecord(id);
            if (objPowerInfo == null)
            {
                return new List<TreeEntity>();
            }
            var objPowerInfoList = objBizPowerInfo.GetList(string.Format(isContainUnDisplay ? "ParentId={0} AND PowerType<>2" : "ParentId={0} AND PowerType<>2 AND IsDisplay=1", objPowerInfo.Id)).OrderBy(c => c.SortIndex).ToList();
            return objPowerInfoList.ConvertToTreeListExceptRoot(id) ?? new List<TreeEntity>();
        }
    }
}
